<?php
function theme_widget_qf_goods(&$setting,&$render){
	if($system->theme){
        $theme_dir = kernel::get_themes_host_url().'/'.$smarty->theme;
    }else{
        $theme_dir = kernel::get_themes_host_url().'/'.app::get('site')->getConf('current_theme');
    }
	$_return['theme_dir']=$theme_dir;
    switch ($setting['selector']) {
    case 'filter':
        parse_str($setting['goods_filter'],$goodsFilter);
        $goodsFilter['goodsOrderBy'] = $setting['goods_order_by'];
        $goodsFilter['goodsNum'] = (intval($setting['limit'])>0)?intval($setting['limit']):6;
        $_return = b2c_widgets::load('Goods')->getGoodsList($goodsFilter);
        //$_return = array_values($_return['goodsRows']);
        break;
    case 'select':
        $goodsFilter['goods_id'] = explode(",", $setting["goods_select"]);
		$goodsFilter['goodsNum'] = (intval($setting['limit'])>0)?intval($setting['limit']):6;		
        $goodsFilter['goodsOrderBy'] = $setting['goods_order_by'];
        $_return = b2c_widgets::load('Goods')->getGoodsList($goodsFilter);
         
        foreach (json_decode($setting['goods_select_linkobj'],1) as $obj) {
            if(trim($obj['pic'])!=""){
                $_return['goodsRows'][$obj['id']]['goodsPicL'] = 
                    $_return['goodsRows'][$obj['id']]['goodsPicM'] = 
                    $_return['goodsRows'][$obj['id']]['goodsPicS'] = $obj['pic'];
            }
            if(trim($obj['nice'])!=""){
                $_return['goodsRows'][$obj['id']]['goodsName'] = $obj['nice'];
            }
        }
        break;

    }
    $gids = array_keys($_return['goodsRows']);
    
    if(!$gids||count($gids) < 1) return $_return;
	
	/*取商品标签和评分 start*/
	if($setting['showgoodstag'] == 'on' || $setting['show_star'] =='on'){
		$pointModel = app::get('b2c')->model('comment_goods_point');
		$point = $pointModel->get_single_point_arr($gids);
		if(is_array($_return['goodsRows'])){
			foreach($_return['goodsRows'] as $k=>$v){
				$_return['goodsRows'][$k]['goods_id'] = $v['goodsId'];
				$_return['goodsRows'][$k]['star'] = $point[$k];
			}
		}
		foreach(kernel::servicelist('tags_special.add') as $services ) {
			if ( is_object($services)) {
				if ( method_exists( $services, 'add') ) {
					$services->add($gids,$_return['goodsRows']);
				}
			}
		}
	}
	/*取商品标签和评分end*/
	//以下代码原本是注释的，现需要取到product_id参数，所以取消注释了bysihua
	 //取多规格数据
    $mdl_product = app::get('b2c')->model('products');
    $products = $mdl_product ->getList('product_id,spec_info,price,freez,store,marketable,goods_id',array('goods_id'=>$gids,'marketable'=>'true'));

    //取用户审核状态、会员价、会员等级
     // @zeng 2017.4.25 当前用户id
    $memberMolde=kernel::single('b2c_user_object');
    $member_id=$memberMolde->get_member_id();
    if($member_id)
    {
        $member_state=$memberMolde->get_member_state($member_id);
        $member_lv_id=$memberMolde->get_member_lv($member_id);
    }
    else
    {
        $member_state='-1';
        $member_lv_id='';
    }
    $data['info']=array('member_state'=>$member_state);
    //@zeng 2017.4.25 商品会员价
    $mlvpriceModel=kernel::single('b2c_goods_object');
    //end


    foreach ($products as $product) {
        $_return['goodsRows'][$product['goods_id']]['products'][] = $product;

        //@zeng 2017.4.25 商品会员价
        $_return['goodsRows'][$product['goods_id']]['products'][] = $data;//用户审核状态
        $mlv_price=$mlvpriceModel->get_member_lv_goods_price($product['goods_id'],$member_lv_id);
        if($mlv_price)
            $_return['goodsRows'][$product['goods_id']]['products'][] = array('mlv_price'=>$mlv_price);//会员价
        else
             $_return['goodsRows'][$product['goods_id']]['products'][] = array('mlv_price'=>$product['price']);//销售价
         
            //@mo add 2017年5月2日 14:50:55
         $member_id = kernel::single('b2c_user_object')->get_member_id();
         if(!empty($member_id))
         {
             $now_can_buy_this_goods_quantity = 0;
               $now_can_buy_this_goods_quantity = kernel::single('b2c_checkproduct_check')->can_buy_productid_quantity($product['product_id']);
				
			 // 将小于触发值的数量提示去掉 add by hzk
               $getstartgiftnum = kernel::single('b2c_checkproduct_check')->getstartgiftnum('product_id',$product['product_id']);
			if($now_can_buy_this_goods_quantity < $getstartgiftnum){
              	$now_can_buy_this_goods_quantity = 999999;
              }
//			echo '<pre>';
//			echo '<br>==========<br>';
//			echo $getstartgiftnum;
//			echo '<br>==========<br>';
				
              if( $now_can_buy_this_goods_quantity!= -1 )  //@mo
              {
                $_return['goodsRows'][$product['goods_id']]['products'][] = array('type'=>app::get('b2c')->_('havegift'));
                $_return['goodsRows'][$product['goods_id']]['products'][] = array('maxbuy'=>$now_can_buy_this_goods_quantity);
                //$products[$key]['gift']['type'] = ;
                //$products[$key]['gift']['maxbuy'] = $now_can_buy_this_goods_quantity;
              }
        }
    } 
    
    	//add by 黄智坤
//       $add['isused'] = (app::get('b2c')->getConf('system.member.IsUsed')) ? (app::get('b2c')->getConf('system.member.IsUsed')) : 0;
//       $add['pernum'] = (app::get('b2c')->getConf('system.product.per.num')) ? (app::get('b2c')->getConf('system.product.per.num')) : 30;
       
	   $add['isused'] = app::get('b2c')->getConf('system.member.IsUsed');
       $add['pernum'] = app::get('b2c')->getConf('system.product.per.num');
       
       $_return['add'] = $add;
	
//	$add['pernum'] = $_SESSION['pernum'];
//    $add['isused'] = $_SESSION['IsUsed'];
       //add 这里是验证登录状态----未登录不显示商品价格 ---黄智坤06-19
		$userObject = kernel::single('b2c_user_object');
        if( $userObject->is_login()){
            $_return['check_login'] = 'login';
        }else{
        	$_return['check_login'] = 'nologin';
        }
        //end
//	$check_login = kernel::single('b2c_user_object')->is_login();
	
//	echo '<pre>';
//	print_r($_return['check_login']);
//	exit;
//	echo '<br>1==';
//	print_r(app::get('b2c')->getConf('system.member.IsUsed'));
//	echo '<br>2==';
//	print_r($_SESSION['pernum']);
//	echo '<br>3==';
//	print_r(app::get('b2c')->getConf('system.product.per.num'));
//	echo '<br>4==';
//	print_r($_return);
//	print_r(array_keys($_return['goodsRows'][92]['peradd']));

//	print_r($aaaa);
//	exit;
		//商品数据需要在/ecstore2.4.0/app/b2c/lib/widgets/goods.php的  protected $_outData = array(。。。）这里加
    return $_return;
}
?>
